package mdm.com.sap;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sap.conn.jco.JCoContext;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoStructure;
import com.sap.conn.jco.JCoTable;

import mdm.com.sap.model.Enquiry_Zstock_Info;
import mdm.com.sap.model.GoodSmvt_item;
import mdm.com.sap.model.Per_Barcode;
import mdm.com.sap.model.Pre_Item;
import mdm.com.sap.model.Return_Result;

public class ModEnquiry {

    private static final Logger LOGGER = LoggerFactory.getLogger(ModEnquiry.class);

    public static Return_Result result = null;

    /**
     * 根据 I_GANGHAO：tankNo号码（缸号） I_color：颜色组 I_3ASIZE：网格值（非必填） I_LGORT：仓位
     * I_BARC：条码 I_EBELN：PO号 I_MATNR物料号表号 查询原料：棉、辅料、布的库存列表
     * 
     * @param barcode
     *            条码
     * @param tankNo
     *            缸号
     * @param color
     *            颜色组
     * @param materialNo
     *            物料号
     * @param grid_value
     *            网格值
     * @param locationID
     *            仓位
     * @param PONo
     *            PO号
     * @return
     */
    public static List<Enquiry_Zstock_Info> ZBAPI_GET_STOCK_INFO_BY_BC(String barcodes, String tankNo, String color,
            String materialNo, String grid_value, String locationID, String PONo, String zbukrs) {
        List<String> barcode = null;
        if (barcodes != null) {
            LOGGER.info(barcodes);
            barcode = SapUtil.stringToLists(barcodes);
        }
        List<Enquiry_Zstock_Info> list = new ArrayList<Enquiry_Zstock_Info>();
        /* String ZBUKRS = "RMIG"; */
        /* ZBUKRS (输入公司代号RMIG/RMI/VN...) */
        
        Map<String, String> map =  new HashMap<String, String>();
        map.put("1", "RMIG");
        map.put("2", "RMI");
        map.put("3", "VN");
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_GET_STOCK_INFO_BY_BC");
            JCoParameterList importParam = function.getImportParameterList();


            importParam.setValue("I_SELECT_BY_PO", "");
            if(zbukrs != null) {
                importParam.setValue("ZBUKRS", map.get(zbukrs));
            }
            
            /* importParam.setValue("ZDFLQ", ZDFLQ); */
            if (!(tankNo == null || "".equals(tankNo))) {
                JCoTable So_MATNR = importParam.getTable("I_GANGHAO");
                So_MATNR.appendRow();
                So_MATNR.setValue("SIGN", "I");
                So_MATNR.setValue("OPTION", "EQ");
                So_MATNR.setValue("LOW", tankNo.toUpperCase());
                So_MATNR.setValue("HIGH", "");
            }
            if (!(color == null || "".equals(color))) {
                if (!"*".equals(color)) {
                    JCoTable So_MATNR = importParam.getTable("I_COLOR");
                    So_MATNR.appendRow();
                    So_MATNR.setValue("SIGN", "I");
                    So_MATNR.setValue("OPTION", "EQ");
                    So_MATNR.setValue("LOW", color.toUpperCase());
                    So_MATNR.setValue("HIGH", "");
                }
            }
            if (!(materialNo == null || "".equals(materialNo))) {
                JCoTable So_MATNR = importParam.getTable("I_MATNR");
                So_MATNR.appendRow();
                So_MATNR.setValue("SIGN", "I");
                So_MATNR.setValue("OPTION", "EQ");
                So_MATNR.setValue("LOW", materialNo.toUpperCase());
                So_MATNR.setValue("HIGH", materialNo.toUpperCase());
            }
            if (!(grid_value == null || "".equals(grid_value))) {
                JCoTable So_MATNR = importParam.getTable("I_3ASIZE");
                So_MATNR.appendRow();
                So_MATNR.setValue("SIGN", "I");
                So_MATNR.setValue("OPTION", "EQ");
                So_MATNR.setValue("LOW", grid_value.toUpperCase());
                So_MATNR.setValue("HIGH", grid_value.toUpperCase());
            }

            if (!(locationID == null || "".equals(locationID))) {
                JCoTable So_MATNR = importParam.getTable("I_LGORT");
                So_MATNR.appendRow();
                So_MATNR.setValue("SIGN", "I");
                So_MATNR.setValue("OPTION", "EQ");
                So_MATNR.setValue("LOW", locationID.toUpperCase());
                So_MATNR.setValue("HIGH", locationID.toUpperCase());
            }

            if (barcode != null) {
                JCoTable I_BARC = importParam.getTable("I_BARC");
                for (int i = 0; i < barcode.size(); i++) {
                    I_BARC.appendRow();
                    I_BARC.setValue("SIGN", "I");
                    I_BARC.setValue("OPTION", "EQ");
                    I_BARC.setValue("LOW", barcode.get(i).toUpperCase());
                    I_BARC.setValue("HIGH", barcode.get(i).toUpperCase());
                }
            }

            if (!(PONo == null || "".equals(PONo))) {
                JCoTable So_MATNR = importParam.getTable("I_PO");
                So_MATNR.appendRow();

                So_MATNR.setValue("SIGN", "I");
                So_MATNR.setValue("OPTION", "EQ");
                So_MATNR.setValue("LOW", PONo.toUpperCase());
                So_MATNR.setValue("HIGH", PONo.toUpperCase());
            }
            function.execute(RfcManager.getDestination());
            JCoTable ZRETURN = function.getTableParameterList().getTable("ZRETURN");
            Return_Result res = SapUtil.GetResult(ZRETURN);
            LOGGER.info("ZBAPI_GET_STOCK_INFO_BY_BC==" + res.getT());
            LOGGER.info("ZBAPI_GET_STOCK_INFO_BY_BC==" + res.getMESSAGE());
            switch (res.getT()) {
                case "S":
                case "":
                    JCoTable ZSTOCK_INFO = function.getTableParameterList().getTable("ZSTOCK_INFO");
                    for (int j = 0; j < ZSTOCK_INFO.getNumRows(); j++) {
                        ZSTOCK_INFO.setRow(j);
                        Enquiry_Zstock_Info info = new Enquiry_Zstock_Info();
                        info.setMatnr(ZSTOCK_INFO.getString("MATNR"));
                        info.setMaktx(ZSTOCK_INFO.getString("MAKTX"));
                        info.setJ_3asize(ZSTOCK_INFO.getString("J_3ASIZE"));
                        info.setAtwtb(ZSTOCK_INFO.getString("ATWTB"));
                        info.setWerks(ZSTOCK_INFO.getString("WERKS"));
                        info.setLgort(ZSTOCK_INFO.getString("LGORT"));
                        info.setMenge(ZSTOCK_INFO.getString("MENGE"));
                        info.setMeins(ZSTOCK_INFO.getString("MEINS"));
                        info.setMsen3(ZSTOCK_INFO.getString("MSEH3"));
                        info.setZsto_type(ZSTOCK_INFO.getString("ZSTO_TYPE"));
                        info.setZbar_code(ZSTOCK_INFO.getString("ZBAR_CODE"));
                        info.setCharg(ZSTOCK_INFO.getString("CHARG"));
                        info.setWgbez(ZSTOCK_INFO.getString("WGBEZ"));
                        info.setZbatch1_po(ZSTOCK_INFO.getString("ZBATCH1_PO"));
                        info.setVbeln(ZSTOCK_INFO.getString("VBELN"));
                        info.setPosnr(ZSTOCK_INFO.getString("POSNR"));
                        info.setZbatch1_qi(ZSTOCK_INFO.getString("ZBATCH1_QI"));
                        info.setZbatch1_batch(ZSTOCK_INFO.getString("ZBATCH1_BATCH"));
                        info.setZbatch1_gr_date(ZSTOCK_INFO.getString("ZBATCH1_GR_DATE"));
                        info.setZmenge_gr(ZSTOCK_INFO.getString("ZMENGE_GR"));
                        info.setZmenge_gi(ZSTOCK_INFO.getString("ZMENGE_GI"));
                        info.setAufnr(ZSTOCK_INFO.getString("AUFNR"));
                        info.setZbatch1_roll(ZSTOCK_INFO.getString("ZBATCH1_ROLL"));
                        info.setZlg_no(ZSTOCK_INFO.getString("ZLG_NO"));
                        list.add(info);
                    }
                    break;
                default:
                    break;
            }
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return list;
    }

   
    public static List<Enquiry_Zstock_Info> ZBAPI_GET_STOCK_INFO_BY_BC(String ZDFLQ,String barcodes, String tankNo, String color,
            String materialNo, String grid_value, String locationID, String PONo, String zbukrs) {
        List<String> barcode = null;
        if (barcodes != null) {
            LOGGER.info(barcodes);
            barcode = SapUtil.stringToLists(barcodes);
        }
        List<Enquiry_Zstock_Info> list = new ArrayList<Enquiry_Zstock_Info>();
        /* String ZBUKRS = "RMIG"; */
        /* ZBUKRS (输入公司代号RMIG/RMI/VN...) */
        
        Map<String, String> map =  new HashMap<String, String>();
        map.put("1", "RMIG");
        map.put("2", "RMI");
        map.put("3", "VN");
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_GET_STOCK_INFO_BY_BC");
            JCoParameterList importParam = function.getImportParameterList();

            importParam.setValue("I_SELECT_BY_PO", "");
            if(zbukrs != null) {
                importParam.setValue("ZBUKRS", map.get(zbukrs));
            }else {
                importParam.setValue("ZBUKRS", "RMIG");
            }
            importParam.setValue("ZDFLQ", ZDFLQ);
            
            if (!(tankNo == null || "".equals(tankNo))) {
                JCoTable So_MATNR = importParam.getTable("I_GANGHAO");
                So_MATNR.appendRow();
                So_MATNR.setValue("SIGN", "I");
                So_MATNR.setValue("OPTION", "EQ");
                So_MATNR.setValue("LOW", tankNo.toUpperCase());
                So_MATNR.setValue("HIGH", "");
            }
            if (!(color == null || "".equals(color))) {
                if (!"*".equals(color)) {
                    JCoTable So_MATNR = importParam.getTable("I_COLOR");
                    So_MATNR.appendRow();
                    So_MATNR.setValue("SIGN", "I");
                    So_MATNR.setValue("OPTION", "EQ");
                    So_MATNR.setValue("LOW", color.toUpperCase());
                    So_MATNR.setValue("HIGH", "");
                }
            }
            if (!(materialNo == null || "".equals(materialNo))) {
                JCoTable So_MATNR = importParam.getTable("I_MATNR");
                So_MATNR.appendRow();
                So_MATNR.setValue("SIGN", "I");
                So_MATNR.setValue("OPTION", "EQ");
                So_MATNR.setValue("LOW", materialNo.toUpperCase());
                So_MATNR.setValue("HIGH", materialNo.toUpperCase());
            }
            if (!(grid_value == null || "".equals(grid_value))) {
                JCoTable So_MATNR = importParam.getTable("I_3ASIZE");
                So_MATNR.appendRow();
                So_MATNR.setValue("SIGN", "I");
                So_MATNR.setValue("OPTION", "EQ");
                So_MATNR.setValue("LOW", grid_value.toUpperCase());
                So_MATNR.setValue("HIGH", grid_value.toUpperCase());
            }

            if (!(locationID == null || "".equals(locationID))) {
                JCoTable So_MATNR = importParam.getTable("I_LGORT");
                So_MATNR.appendRow();
                So_MATNR.setValue("SIGN", "I");
                So_MATNR.setValue("OPTION", "EQ");
                So_MATNR.setValue("LOW", locationID.toUpperCase());
                So_MATNR.setValue("HIGH", locationID.toUpperCase());
            }

            if (barcode != null) {
                JCoTable I_BARC = importParam.getTable("I_BARC");
                for (int i = 0; i < barcode.size(); i++) {
                    I_BARC.appendRow();
                    I_BARC.setValue("SIGN", "I");
                    I_BARC.setValue("OPTION", "EQ");
                    I_BARC.setValue("LOW", barcode.get(i).toUpperCase());
                    I_BARC.setValue("HIGH", barcode.get(i).toUpperCase());
                }
            }

            if (!(PONo == null || "".equals(PONo))) {
                JCoTable So_MATNR = importParam.getTable("I_PO");
                So_MATNR.appendRow();
                So_MATNR.setValue("SIGN", "I");
                So_MATNR.setValue("OPTION", "EQ");
                So_MATNR.setValue("LOW", PONo.toUpperCase());
                So_MATNR.setValue("HIGH", PONo.toUpperCase());
            }
            function.execute(RfcManager.getDestination());
            JCoTable ZRETURN = function.getTableParameterList().getTable("ZRETURN");
            result = SapUtil.GetResult(ZRETURN);
            LOGGER.info("ZBAPI_GET_STOCK_INFO_BY_BC==" + result.getT());
            LOGGER.info("ZBAPI_GET_STOCK_INFO_BY_BC==" + result.getMESSAGE());
            switch (result.getT()) {
                case "S":
                case "":
                    JCoTable ZSTOCK_INFO = function.getTableParameterList().getTable("ZSTOCK_INFO");
                    for (int j = 0; j < ZSTOCK_INFO.getNumRows(); j++) {
                        ZSTOCK_INFO.setRow(j);
                        Enquiry_Zstock_Info info = new Enquiry_Zstock_Info();
                        info.setMatnr(ZSTOCK_INFO.getString("MATNR"));
                        info.setMaktx(ZSTOCK_INFO.getString("MAKTX"));
                        info.setJ_3asize(ZSTOCK_INFO.getString("J_3ASIZE"));
                        info.setAtwtb(ZSTOCK_INFO.getString("ATWTB"));
                        info.setWerks(ZSTOCK_INFO.getString("WERKS"));
                        info.setLgort(ZSTOCK_INFO.getString("LGORT"));
                        info.setMenge(ZSTOCK_INFO.getString("MENGE"));
                        info.setMeins(ZSTOCK_INFO.getString("MEINS"));
                        info.setMsen3(ZSTOCK_INFO.getString("MSEH3"));
                        info.setZsto_type(ZSTOCK_INFO.getString("ZSTO_TYPE"));
                        info.setZbar_code(ZSTOCK_INFO.getString("ZBAR_CODE"));
                        info.setCharg(ZSTOCK_INFO.getString("CHARG"));
                        info.setWgbez(ZSTOCK_INFO.getString("WGBEZ"));
                        info.setZbatch1_po(ZSTOCK_INFO.getString("ZBATCH1_PO"));
                        info.setVbeln(ZSTOCK_INFO.getString("VBELN"));
                        info.setPosnr(ZSTOCK_INFO.getString("POSNR"));
                        info.setZbatch1_qi(ZSTOCK_INFO.getString("ZBATCH1_QI"));
                        info.setZbatch1_batch(ZSTOCK_INFO.getString("ZBATCH1_BATCH"));
                        info.setZbatch1_gr_date(ZSTOCK_INFO.getString("ZBATCH1_GR_DATE"));
                        info.setZmenge_gr(ZSTOCK_INFO.getString("ZMENGE_GR"));
                        info.setZmenge_gi(ZSTOCK_INFO.getString("ZMENGE_GI"));
                        info.setAufnr(ZSTOCK_INFO.getString("AUFNR"));
                        info.setZbatch1_roll(ZSTOCK_INFO.getString("ZBATCH1_ROLL"));
                        info.setZlg_no(ZSTOCK_INFO.getString("ZLG_NO"));
                        list.add(info);
                    }
                    break;
                default:
                    break;
            }
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return list;
    }
    
    /**
     * 条码转架
     * @param item_list  条码集合
     * @return
     */
    public static List<Return_Result> AfsGoodsMvtCreate(List<GoodSmvt_item> item_list) {
        LOGGER.info("=======AfsGoodsMvtCreate====");
        List<Return_Result> resultlist =  new ArrayList<Return_Result>();
        try {
            JCoFunction function = RfcManager.getFunction("/AFS/BAPI_GOODSMVT_CREATE");
            JCoFunction commitFunction = RfcManager.getFunction("BAPI_TRANSACTION_COMMIT");
            JCoParameterList importParam = function.getImportParameterList();
            
            JCoStructure GOODSMVT_HEADER = importParam.getStructure("GOODSMVT_HEADER");
            JCoStructure GOODSMVT_CODE = importParam.getStructure("GOODSMVT_CODE");
            GOODSMVT_HEADER.setValue("PSTNG_DATE", SapUtil.nowDateString());
            GOODSMVT_HEADER.setValue("DOC_DATE", SapUtil.nowDateString());
            GOODSMVT_CODE.setValue("GM_CODE", "04");
            JCoTable AFS_GOODSMVT_ITEM = function.getTableParameterList().getTable("AFS_GOODSMVT_ITEM");
            
            for(GoodSmvt_item item : item_list) {
                AFS_GOODSMVT_ITEM.appendRow();
                AFS_GOODSMVT_ITEM.setValue("MATERIAL", item.getMATERIAL());
                AFS_GOODSMVT_ITEM.setValue("PLANT", item.getPLANT());
                AFS_GOODSMVT_ITEM.setValue("STGE_LOC", item.getSTGE_LOC());
                AFS_GOODSMVT_ITEM.setValue("BATCH", item.getBATCH());
                AFS_GOODSMVT_ITEM.setValue("GRID_VALUE", item.getGRID_VALUE());
                AFS_GOODSMVT_ITEM.setValue("MOVE_TYPE", item.getMOVE_TYPE());
                AFS_GOODSMVT_ITEM.setValue("ENTRY_QNT", item.getENTRY_QNT());
                AFS_GOODSMVT_ITEM.setValue("SPEC_STOCK", item.getSPEC_STOCK());
                AFS_GOODSMVT_ITEM.setValue("VAL_S_ORD_ITEM", item.getVAL_S_ORD_ITEM());
                AFS_GOODSMVT_ITEM.setValue("VAL_SALES_ORD", item.getVAL_SALES_ORD());
                AFS_GOODSMVT_ITEM.setValue("MOVE_STLOC", item.getMOVE_STLOC());
                AFS_GOODSMVT_ITEM.setValue("MOVE_BATCH", item.getBATCH());
//                AFS_GOODSMVT_ITEM.setValue("ITEM_TEXT", item.getSTGE_LOC());
            }
            JCoContext.begin(RfcManager.getDestination());
            function.execute(RfcManager.getDestination());
            JCoTable ZRETURN = function.getTableParameterList().getTable("RETURN");
            String DocumentNo = function.getExportParameterList().getString("MATERIALDOCUMENT");
            LOGGER.info("====DocumentNo==="+DocumentNo);
            boolean bSuccess = true;
            LOGGER.info("====ZRETURN==="+ZRETURN.getNumRows());
            if(ZRETURN.getNumRows() == 0) {
                Return_Result t = new Return_Result();
                t.setT("IDTech");
                t.setMESSAGE(DocumentNo);
                resultlist.add(t);
            }
            for(int i=0;i<ZRETURN.getNumRows();i++) {
                ZRETURN.setRow(i);
                LOGGER.info("type==============="+ZRETURN.getString("TYPE"));
                LOGGER.info("message==============="+ZRETURN.getString("MESSAGE"));
                if(ZRETURN.getString("TYPE").equals("")) {
                    Return_Result t = new Return_Result();
                    t.setT("IDTech");
                    t.setID(DocumentNo);
                    t.setMESSAGE(DocumentNo);
                    t.setNUM(ZRETURN.getString("NUMBER"));
                    resultlist.add(t);
                }else {
                    bSuccess = false;
                    Return_Result t = new Return_Result();
                    t.setT(ZRETURN.getString("TYPE"));
                    t.setID(ZRETURN.getString("ID"));
                    t.setMESSAGE(ZRETURN.getString("MESSAGE"));
                    t.setNUM(ZRETURN.getString("NUMBER"));
                    resultlist.add(t);
                }
            }
            //有条码转架失败 则不提交事务
            if(bSuccess) {
                commitFunction.execute(RfcManager.getDestination());
                JCoContext.end(RfcManager.getDestination());
            }
            
        } catch (JCoException e) {
            e.printStackTrace();
            Return_Result t = new Return_Result();
            t.setT("E");
            t.setID("BAPI_GOODSMVT_CREATE");
            t.setMESSAGE(e.getMessage());
            resultlist.add(t);
        }
        return resultlist;
    }
    
    /**
     * 转架位
     * 
     * @param enquiry_Zstock_Info
     * @param TargetLocation
     * @return
     */
    public static Return_Result AFS_BAPI_GOODSMVT_CREATE(String strs, String TargetLocation) {
        Return_Result res = null;
        List<Enquiry_Zstock_Info> enquiry_Zstock_Info = null;
        if (strs != null) {
            enquiry_Zstock_Info = SapUtil.stringToList(strs, Enquiry_Zstock_Info.class);
        }
        try {
            JCoFunction function = RfcManager.getFunction("/AFS/BAPI_GOODSMVT_CREATE");
            JCoFunction commitFunction = RfcManager.getFunction("BAPI_TRANSACTION_COMMIT");
            
            JCoParameterList importParam = function.getImportParameterList();
            JCoStructure GOODSMVT_HEADER = importParam.getStructure("GOODSMVT_HEADER");
            JCoStructure GOODSMVT_CODE = importParam.getStructure("GOODSMVT_CODE");
            GOODSMVT_HEADER.setValue("PSTNG_DATE", SapUtil.nowDateString());
            GOODSMVT_HEADER.setValue("DOC_DATE", SapUtil.nowDateString());
            GOODSMVT_CODE.setValue("GM_CODE", "04");
            
            if (enquiry_Zstock_Info.size() > 0) {
                JCoTable AFS_GOODSMVT_ITEM = function.getTableParameterList().getTable("AFS_GOODSMVT_ITEM");
                for (Enquiry_Zstock_Info str : enquiry_Zstock_Info) {
                    AFS_GOODSMVT_ITEM.appendRow();
                    AFS_GOODSMVT_ITEM.setValue("MATERIAL", str.getMatnr());
                    AFS_GOODSMVT_ITEM.setValue("PLANT", str.getWerks());
                    AFS_GOODSMVT_ITEM.setValue("STGE_LOC", str.getLgort());
                    AFS_GOODSMVT_ITEM.setValue("BATCH", str.getCharg());
                    AFS_GOODSMVT_ITEM.setValue("GRID_VALUE", str.getJ_3asize());
                    AFS_GOODSMVT_ITEM.setValue("ENTRY_QNT", str.getMenge());
                    AFS_GOODSMVT_ITEM.setValue("ITEM_TEXT", TargetLocation);

                    if (!(str.getVbeln().equals("") || str.getVbeln() == null)) {
                        AFS_GOODSMVT_ITEM.setValue("SPEC_STOCK", "E");
                        AFS_GOODSMVT_ITEM.setValue("VAL_S_ORD_ITEM", str.getPosnr());
                        AFS_GOODSMVT_ITEM.setValue("VAL_SALES_ORD", str.getVbeln());
                    } else {
                        AFS_GOODSMVT_ITEM.setValue("SPEC_STOCK", "");
                        AFS_GOODSMVT_ITEM.setValue("VAL_S_ORD_ITEM", "");
                        AFS_GOODSMVT_ITEM.setValue("VAL_SALES_ORD", "");
                    }

                    switch (str.getZsto_type()) {
                        case "非限制库存":
                            AFS_GOODSMVT_ITEM.setValue("MOVE_TYPE", "311");
                            LOGGER.info("a" + 311);
                            break;
                        case "QI库存":
                            AFS_GOODSMVT_ITEM.setValue("MOVE_TYPE", "323");
                            LOGGER.info("a" + 323);
                            break;
                        case "冻结库存":
                            AFS_GOODSMVT_ITEM.setValue("MOVE_TYPE", "325");
                            LOGGER.info("a" + 325);
                            break;
                        default:
                            AFS_GOODSMVT_ITEM.setValue("MOVE_TYPE", "311");
                            LOGGER.info("b" + 311);
                            break;
                    }
                    if (!(TargetLocation == null || "".equals(TargetLocation))) {
                        AFS_GOODSMVT_ITEM.setValue("MOVE_STLOC", TargetLocation.toUpperCase());
                    } else {
                        AFS_GOODSMVT_ITEM.setValue("MOVE_STLOC", str.getLgort());
                    }

                }
            }


            JCoContext.begin(RfcManager.getDestination());
            function.execute(RfcManager.getDestination());

            JCoTable ZRETURN = function.getTableParameterList().getTable("RETURN");
            res = SapUtil.GetResult(ZRETURN);

            String str = null;
            if (res.getT() == null || res.getT().equals("")) {
                JCoStructure GOODSMVT_HEADRET = function.getExportParameterList().getStructure("GOODSMVT_HEADRET");
                str = GOODSMVT_HEADRET.getString("MAT_DOC");
                LOGGER.info(str);
            }
            
            commitFunction.execute(RfcManager.getDestination());
            JCoContext.end(RfcManager.getDestination());
            LOGGER.info("/AFS/BAPI_GOODSMVT_CREATE====Type" + res.getT());
            LOGGER.info("/AFS/BAPI_GOODSMVT_CREATE====MESSAGE" + res.getMESSAGE());
            switch (res.getT()) {
                case "E":
                    break;
                case "":
                    res.setT("S");
                    res.setMESSAGE(str);
                    break;
                default:
                    break;

            }
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 转待领区-新
     * 
     * @param strs
     *            list详情
     * @param pMachineNo
     *            备料单号
     * @return
     */
    public static Return_Result ZBAPI_PREORD_GDS_MVT(String strs, String pMachineNo) {
        LOGGER.info("=============ZBAPI_PREORD_GDS_MVT================");
        Return_Result res = new Return_Result();
        List<Pre_Item> pre_Item = null;
        int iProcessedBarcode = 0;
        double totalQTY = 0;
        String targetPrepareNo = "";
        String StatusChange = "";
        boolean isSummaryOrder = false;
        List<String> sSuccessedItem = new ArrayList<String>();
        List<String> sFailedItem = new ArrayList<String>();
        String errorType = "";
        if (strs != null) {
            pre_Item = SapUtil.stringToList(strs, Pre_Item.class);
        }
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_PREORD_GDS_MVT");
            JCoParameterList importParam = function.getImportParameterList();
            
            JCoTable IT_SERNR = function.getTableParameterList().getTable("IT_SERNR");
            if (pre_Item.size() > 0) {
                
                for (Pre_Item str : pre_Item) {
                    System.out.println("==========getBarCodeList============="+str.getBarCodeList());
                    
                    if (str.getCotnr() == null || str.getCotnr().equals("")) {
                        targetPrepareNo = str.getPmtnr();
                    } else {
                        isSummaryOrder = true;
                        targetPrepareNo = str.getCotnr();
                    }
                    if(str.getBarCodeList() != null) {
                        iProcessedBarcode = iProcessedBarcode + str.getBarCodeList().size();
                        if (str.getBarCodeList().size() > 60) {
                            throw new Exception("XX");
                        }
                        if(!isSummaryOrder) {
                            totalQTY = 0;
                            IT_SERNR.clear();
                        }
                        
                        for (Per_Barcode per_Barcode : str.getBarCodeList()) {
                            importParam.setValue("PRITM", str.getPritm());
                            IT_SERNR.appendRow();
                            IT_SERNR.setValue("SERNR", per_Barcode.getZbar_code());
                            IT_SERNR.setValue("MENGE", per_Barcode.getT4());
//                            totalQTY = totalQTY + Double.parseDouble(per_Barcode.getPre_qty());
                            totalQTY = totalQTY + Double.parseDouble(per_Barcode.getT4());
                            DecimalFormat df = new DecimalFormat("#.000");
                            String formatQTY = df.format(totalQTY);
                            totalQTY = Double.parseDouble(formatQTY);
                        }
                        if (!isSummaryOrder) {
                            importParam.setValue("PMTNR", targetPrepareNo);
//                            importParam.setValue("I_SGTXT", pMachineNo);
                            importParam.setValue("QUANTITY", String.valueOf(totalQTY));
                            function.execute(RfcManager.getDestination());
                            StatusChange = function.getExportParameterList().getString("STATUS");
                            JCoTable returntable = function.getTableParameterList().getTable("IT_RETURN");
                            res = SapUtil.GetResult(returntable);
                            LOGGER.info("---------------------"+res.ToString());
                            if(res.getT().equals("S") || res.getT().equals("") ) {
                                if(res.getMESSAGE()!=null) {
                                    if(res.getMESSAGE().contains("49")) {
                                        sSuccessedItem.add(targetPrepareNo);
                                    }
                                }
                            }else {
                                sFailedItem.add(String.format("%s[%s]", targetPrepareNo, res.getMESSAGE()));
                            }
                        }
                    }
                }
            }
            if(isSummaryOrder) {
                importParam.setValue("PMTNR", targetPrepareNo);
                importParam.setValue("QUANTITY", String.valueOf(totalQTY));
                function.execute(RfcManager.getDestination());
                StatusChange = function.getExportParameterList().getString("STATUS");
                JCoTable returntable = function.getTableParameterList().getTable("IT_RETURN");
                res = SapUtil.GetResult(returntable);
                if(res.getT().equals("S") || res.getT().equals("") ) {
                    LOGGER.info("========================="+res.ToString());
                    if(res.getMESSAGE()!=null) {
                        if(res.getMESSAGE().contains("49")) {
                            sSuccessedItem.add(targetPrepareNo);
                        }
                    }
                }else {
                    sFailedItem.add(String.format("%s[%s]", targetPrepareNo, res.getMESSAGE()));
                }
            }
           
            if(sFailedItem.size() == 0) {
                if(sSuccessedItem.size() > 0) {
                    if(!StatusChange.equals("")) {
                        res.setNUM(StatusChange);
                    }
                }else {
                    throw new Exception("ZZ");
                }
            }else {
                res.setT("S");
                if (sSuccessedItem.size() > 0) {
                    res.setMESSAGE("1"); //部分转仓成功
                }
                String _message = "";
                for(String s : sFailedItem) {
                    if(_message.equals("")) {
                        _message=s;
                    } else {
                        _message=_message+","+s;
                    }
                }
                if(sFailedItem.size() > 0) {
                    res.setT("E");
                    res.setNUM("ERROR0");
                    res.setMESSAGE(_message);
                }
            }
        } catch (Exception e) {
//            e.printStackTrace();
            switch (e.getMessage())
            {
                case "ZZ":
                    res.setT("E");
                    res.setID("ZBAPI_PREORD_GDS_MVT");
                    res.setNUM("ERROR1");//不能操作备料单{0}，条码量不能为0条
                    res.setMESSAGE(targetPrepareNo);
                    break;
                case "XX":
                    res.setT("E");
                    res.setID("ZBAPI_PREORD_GDS_MVT");
                    res.setNUM("ERROR2");//不能操作备料单{0}，条码量不能超过60条！
                    res.setMESSAGE(targetPrepareNo);
                    break;
                default:
                    res.setT("E");
                    res.setID("ZBAPI_PREORD_GDS_MVT");
                    res.setNUM("");
                    res.setMESSAGE(e.getMessage());
                    break;
            }
        }
        return res;
    }

    /**
     * 备料单号:prepareNo
     * 
     * @param prepareNo
     * @return
     */
    public static List<Pre_Item> ZBAPI_PREORD_GET_PRE_ITEM(String prepareNo) {
        List<Pre_Item> list = new ArrayList<Pre_Item>();
        ZBAPI_PREORD_UNLOCK(prepareNo);
//        boolean bCallSuccess = true;
        try {
            
            long startTime=System.currentTimeMillis(); 
            
            JCoFunction function = RfcManager.getFunction("ZBAPI_PREORD_GET_PRE_ITEM");
            JCoParameterList importParam = function.getImportParameterList();
            importParam.setValue("PRENO", prepareNo);
            importParam.setValue("LOCK_FLAG", "X");
            importParam.setValue("AUTO_CANCEL", "X");
            function.execute(RfcManager.getDestination());
            
            long endTime=System.currentTimeMillis();
            
            LOGGER.info("sap execute time :"+(endTime-startTime)+"ms");
            
            result = SapUtil.GetResult(function.getTableParameterList().getTable("IT_RETURN"));
            LOGGER.info("ZBAPI_PREORD_GET_PRE_ITEM==" + result.getT());
            LOGGER.info("ZBAPI_PREORD_GET_PRE_ITEM==" + result.getMESSAGE());

            JCoTable PRE_STAT = function.getTableParameterList().getTable("PRE_STAT");
            
            if(PRE_STAT.getNumRows() > 1) {
                Pre_Item info = new Pre_Item();
                info.setPmtnr("ERROR");
                info.setPritm("Q");
                list.add(info);
                result.setT("");
                result.setMESSAGE("");
            }else {
                for (int j = 0; j < PRE_STAT.getNumRows(); j++) {
                    PRE_STAT.setRow(j);
                    
                    switch(PRE_STAT.getString("STATUS").trim()) {
                        case "2":
                            break;
                        default:
                            Pre_Item info = new Pre_Item();
                            info.setPmtnr("ERROR");
                            info.setPritm("P");
                            list.add(info);
                            result.setT("");
                            result.setMESSAGE("");
                            break;
                    }
                }
            }

            if(list.size() == 0) {
                switch (result.getT()) {
                    case "S":
                    case "":
                        
                        long start=System.currentTimeMillis(); 
                        
                        JCoTable PRE_ITEM = function.getTableParameterList().getTable("PRE_ITEM");
                        for (int j = 0; j < PRE_ITEM.getNumRows(); j++) {
                            PRE_ITEM.setRow(j);
                            Pre_Item info = new Pre_Item();
                            info.setPmtnr(PRE_ITEM.getString("PMTNR"));
                            info.setPritm(PRE_ITEM.getString("PRITM"));
                            info.setCotnr(PRE_ITEM.getString("COTNR"));
                            info.setCoitm(PRE_ITEM.getString("COITM"));
                            info.setMatnr(PRE_ITEM.getString("MATNR"));
                            info.setJ_3asize(PRE_ITEM.getString("J_3ASIZE"));
                            info.setMaktx(PRE_ITEM.getString("MAKTX"));
                            info.setAtwtb(PRE_ITEM.getString("ATWTB"));
                            info.setWmeng(PRE_ITEM.getString("WMENG"));
                            info.setPmeng(PRE_ITEM.getString("PMENG"));
                            info.setEnmng(PRE_ITEM.getString("ENMNG"));
                            info.setKalab(PRE_ITEM.getString("KALAB"));
                            info.setMseh3(PRE_ITEM.getString("MSEH3"));
                            info.setMtart(PRE_ITEM.getString("MTART"));
                            info.setCharg(PRE_ITEM.getString("CHARG"));
                            info.setSernr(PRE_ITEM.getString("SERNR"));
                            info.setCo_grp(PRE_ITEM.getString("CO_GRP"));
                            info.setGa_no(PRE_ITEM.getString("GA_NO"));
                            info.setPrepred_qty("0");
//                            List<Per_Barcode> barcode = ZBAPI_GET_SERIAL_GI(PRE_ITEM.getString("PMTNR"),
//                                    PRE_ITEM.getString("PRITM"));
//                            info.setBarCodeList(barcode);
                            list.add(info);
                        }
                        long end=System.currentTimeMillis(); 
                        LOGGER.info("for in time : "+(end-start)+"ms");
                        
                        break;
                    default:
                        break;
                }
            }
        } catch (JCoException e) {
            e.printStackTrace();
        } finally {
            ZBAPI_PREORD_UNLOCK(prepareNo);
        }
        return list;
    }
    
    
    /**
     * 发料单号:prepareNo
     * 
     * @param prepareNo
     * @return
     */
    public static List<Pre_Item> FZBAPI_PREORD_GET_PRE_ITEM(String prepareNo) {
        List<Pre_Item> list = new ArrayList<Pre_Item>();
        
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_PREORD_GET_PRE_ITEM");
            JCoParameterList importParam = function.getImportParameterList();
            importParam.setValue("PRENO", prepareNo);
            importParam.setValue("LOCK_FLAG", "X");
            importParam.setValue("AUTO_CANCEL", "X");
            function.execute(RfcManager.getDestination());
            result = SapUtil.GetResult(function.getTableParameterList().getTable("IT_RETURN"));
            LOGGER.info("ZBAPI_PREORD_GET_PRE_ITEM==" + result.getT());
            LOGGER.info("ZBAPI_PREORD_GET_PRE_ITEM==" + result.getMESSAGE());

            JCoTable PRE_STAT = function.getTableParameterList().getTable("PRE_STAT");
            
            if(PRE_STAT.getNumRows() > 1) {
                Pre_Item info = new Pre_Item();
                info.setPmtnr("ERROR");
                info.setPritm("Q");//已经被提出取消
                list.add(info);
                result.setT("");
                result.setMESSAGE("");
            }else {
                for (int j = 0; j < PRE_STAT.getNumRows(); j++) {
                    PRE_STAT.setRow(j);
                    
                    switch(PRE_STAT.getString("STATUS").trim()) {
                        case "4":
                        case "5":
                            break;
                        default:
                            Pre_Item info = new Pre_Item();
                            info.setPmtnr("ERROR");
                            info.setPritm("P");//状态不匹配
                            list.add(info);
                            result.setT("");
                            result.setMESSAGE("");
                            break;
                    }
                }
            }

            if(list.size() == 0) {
                switch (result.getT()) {
                    case "S":
                    case "":
                        JCoTable PRE_ITEM = function.getTableParameterList().getTable("PRE_ITEM");
                        
                        for (int j = 0; j < PRE_ITEM.getNumRows(); j++) {
                            PRE_ITEM.setRow(j);
                            Pre_Item info = new Pre_Item();
                            info.setPmtnr(PRE_ITEM.getString("PMTNR"));
                            info.setPritm(PRE_ITEM.getString("PRITM"));
                            info.setCotnr(PRE_ITEM.getString("COTNR"));
                            info.setCoitm(PRE_ITEM.getString("COITM"));
                            info.setMatnr(PRE_ITEM.getString("MATNR"));
                            info.setJ_3asize(PRE_ITEM.getString("J_3ASIZE"));
                            info.setMaktx(PRE_ITEM.getString("MAKTX"));
                            info.setAtwtb(PRE_ITEM.getString("ATWTB"));
                            info.setWmeng(PRE_ITEM.getString("WMENG"));
                            info.setPmeng(PRE_ITEM.getString("PMENG"));
                            info.setEnmng(PRE_ITEM.getString("ENMNG"));
                            info.setKalab(PRE_ITEM.getString("KALAB"));
                            info.setMseh3(PRE_ITEM.getString("MSEH3"));
                            info.setMtart(PRE_ITEM.getString("MTART"));
                            info.setCharg(PRE_ITEM.getString("CHARG"));
                            info.setSernr(PRE_ITEM.getString("SERNR"));
                            info.setCo_grp(PRE_ITEM.getString("CO_GRP"));
                            info.setGa_no(PRE_ITEM.getString("GA_NO"));
                            info.setPrepred_qty("0");
                            List<Per_Barcode> barcode = ZBAPI_GET_SERIAL_GI(PRE_ITEM.getString("PMTNR"),
                                    PRE_ITEM.getString("PRITM"));
                            info.setBarCodeList(barcode);
                            list.add(info);
                        }
                        break;
                    default:
                        break;
                }
            }
        } catch (JCoException e) {
            e.printStackTrace();
        } finally {
            ZBAPI_PREORD_UNLOCK(prepareNo);
        }
        return list;
    }

    /**
     * 条码详情
     * 
     * @param pPmtnr
     * @param pritm
     * @return
     */
    public static List<Per_Barcode> ZBAPI_GET_SERIAL_GI(String pPmtnr, String pritm) {
        List<Per_Barcode> list = new ArrayList<Per_Barcode>();
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_GET_SERIAL_GI");
            JCoParameterList importParam = function.getImportParameterList();
            importParam.setValue("PMTNR", pPmtnr);
            importParam.setValue("PRITM", pritm);
            function.execute(RfcManager.getDestination());
            JCoTable STOCK_OUT = function.getTableParameterList().getTable("STOCK_OUT");

            for (int j = 0; j < STOCK_OUT.getNumRows(); j++) {
                STOCK_OUT.setRow(j);
                Per_Barcode info = new Per_Barcode();
                info.setBarcode(STOCK_OUT.getString("SERNR"));
                info.setPre_qty(STOCK_OUT.getString("MENGE"));
                info.setMpre_qty(STOCK_OUT.getString("MENGE"));
                list.add(info);
            }
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 根据条形码和数量新增物料
     * 
     * @param pOrderNo
     * @param pMaterialNo
     * @return
     */
    public static Return_Result ZBAPI_INV_FOR_ORDER(String pOrderNo, String pMaterialNo, String pGridValue,
            String pGangNo, String pColorNo) {
        Return_Result res = null;
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_INV_FOR_ORDER");
            JCoParameterList importParam = function.getImportParameterList();
            importParam.setValue("ORDER_NUMBER", pOrderNo);
            importParam.setValue("MATERIAL", pMaterialNo);
            if (!(pGridValue == null || "".equals(pGridValue))) {
                JCoTable So_MATNR = importParam.getTable("OPT_J_3ASIZE");
                /* Create new MATNRSELECTION row */
                So_MATNR.appendRow();
                /* Populate current MATNRSELECTION row with data from list */
                So_MATNR.setValue("SIGN", "I");
                So_MATNR.setValue("OPTION", "EQ");
                So_MATNR.setValue("LOW", pGridValue.toUpperCase());
                So_MATNR.setValue("HIGH", "");
            }
            if (!(pGangNo == null || "".equals(pGangNo))) {
                JCoTable So_MATNR = importParam.getTable("OPT_GA_NO");
                /* Create new MATNRSELECTION row */
                So_MATNR.appendRow();
                /* Populate current MATNRSELECTION row with data from list */
                So_MATNR.setValue("SIGN", "I");
                So_MATNR.setValue("OPTION", "EQ");
                So_MATNR.setValue("LOW", pGangNo.toUpperCase());
                So_MATNR.setValue("HIGH", "");
            }
            if (!(pColorNo == null || "".equals(pColorNo))) {
                JCoTable So_MATNR = importParam.getTable("OPT_CO_GP");
                /* Create new MATNRSELECTION row */
                So_MATNR.appendRow();
                /* Populate current MATNRSELECTION row with data from list */
                So_MATNR.setValue("SIGN", "I");
                So_MATNR.setValue("OPTION", "EQ");
                So_MATNR.setValue("LOW", pColorNo);
                So_MATNR.setValue("HIGH", "");
            }
            function.execute(RfcManager.getDestination());
            res = SapUtil.GetResult(function.getExportParameterList().getStructure("RETURN"));
            LOGGER.info(res.getT());
            LOGGER.info(res.getMESSAGE());
            switch (res.getT()) {
                case "S":
                case "":
                    break;
                default:
                    break;
            }

        } catch (JCoException e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 备料完成
     * 
     * @param pPrepMaterialNo
     * @return
     */
    public static Return_Result ZBAPI_PREORD_STS_CHG(String pMachineNo) {
        Return_Result res = null;
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_PREORD_STS_CHG");
            JCoParameterList importParam = function.getImportParameterList();
            importParam.setValue("PRENO", pMachineNo);
            importParam.setValue("STATUS", "3");
            importParam.setValue("FLAG", "U");
            function.execute(RfcManager.getDestination());
            res = SapUtil.GetResult(function.getExportParameterList().getStructure("RETURN"));
            LOGGER.info(res.getT());
            LOGGER.info(res.getMESSAGE());

        } catch (JCoException e) {
            e.printStackTrace();
        }
        return res;
    }

    
    /**
     * 备料完成 （新）
     * 
     * @param pPrepMaterialNo
     * @return
     */
    public static Return_Result ZBAPI_PREORD_STS_CHG(String pMachineNo,String reasondesc,String status) {
        Return_Result res = null;
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_PREORD_STS_CHG");
            JCoParameterList importParam = function.getImportParameterList();
            importParam.setValue("PRENO", pMachineNo);
            importParam.setValue("REASON", reasondesc);
            importParam.setValue("STATUS", status);
            importParam.setValue("FLAG", "U");
            function.execute(RfcManager.getDestination());
            res = SapUtil.GetResult(function.getExportParameterList().getStructure("RETURN"));
            LOGGER.info(res.getT());
            LOGGER.info(res.getMESSAGE());
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return res;
    }
    
    /**
     * 释放备料ID
     * 
     * @param pPRENO
     */
    public static void ZBAPI_PREORD_UNLOCK(String pPRENO) {
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_PREORD_UNLOCK");
            JCoParameterList importParam = function.getImportParameterList();
            importParam.setValue("PRENO", pPRENO);
            function.execute(RfcManager.getDestination());

        } catch (JCoException e) {
            e.printStackTrace();
        }
    }

    /**
     * 领料
     * 
     * @param prepareNo
     * @param id
     * @return
     */
    public static Return_Result ZBAPI_VER_ID(String prepareNo, String id) {
        Return_Result res = new Return_Result();
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_VER_ID");
            JCoParameterList importParam = function.getImportParameterList();
            importParam.setValue("MRDPT", prepareNo);
            importParam.setValue("DPTID", id);
            function.execute(RfcManager.getDestination());

            String ret = function.getExportParameterList().getString("RETURN");

            if (ret.equals("")) {
                res.setT("S");
                res.setMESSAGE("Success");
            } else {
                res.setT("E");
                res.setMESSAGE("Failed");
            }

        } catch (JCoException e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 发料
     * 
     * @param prepareNo
     * @param id
     * @return
     */
    public static Return_Result ZBAPI_PREORD_GI(String prepareNo, String id, String strs) {
        Return_Result res = new Return_Result();
        String targetPrepareNo = "";
//        boolean bPreparationCompleted = true;
        List<Pre_Item> list = null;
        if (strs != null) {
            list = SapUtil.stringToList(strs, Pre_Item.class);
        }
        try {
            double totalQty = 0;
            JCoFunction function = RfcManager.getFunction("ZBAPI_PREORD_GI");
            JCoTable T_STOCK_OUT = function.getTableParameterList().getTable("STOCK_OUT");
            T_STOCK_OUT.clear();
            
            JCoParameterList importParam = function.getImportParameterList();
            if (list.size() > 0) {
                for (Pre_Item pre_Item : list) {
//                    if(Double.parseDouble(pre_Item.getPmeng()) > Double.parseDouble(pre_Item.getEnmng())) {
//                        bPreparationCompleted = false;
//                    }
                    if(pre_Item.getCotnr().equals("")) {
                        targetPrepareNo = pre_Item.getPmtnr();
                    }else {
                        targetPrepareNo = pre_Item.getCotnr();
                    }
//                    importParam.setValue("I_SGTXT", prepareNo);
                    importParam.setValue("DPTID", id);
                    importParam.setValue("PMTNR", pre_Item.getPmtnr());
                    importParam.setValue("PRITM", pre_Item.getPritm());
                    if(pre_Item.getBarCodeList() != null) {
                        for (int j = 0; j < pre_Item.getBarCodeList().size(); j++) {
                            totalQty = totalQty + Double.parseDouble(pre_Item.getBarCodeList().get(j).getMpre_qty());
                            DecimalFormat df = new DecimalFormat("#.000");
                            String str = df.format(totalQty);
                            totalQty = Double.parseDouble(str);
                            T_STOCK_OUT.appendRow();
                            T_STOCK_OUT.setValue("PMTNR", pre_Item.getPmtnr());
                            T_STOCK_OUT.setValue("PRITM", pre_Item.getPritm());
                            T_STOCK_OUT.setValue("SERNR", pre_Item.getBarCodeList().get(j).getBarcode());
                            T_STOCK_OUT.setValue("MENGE", pre_Item.getBarCodeList().get(j).getMpre_qty());
                        }
                    }
                    importParam.setValue("QUANTITY", String.valueOf(totalQty));
                }
            }
            
            function.execute(RfcManager.getDestination());
            
            JCoTable IT_RETURN = function.getTableParameterList().getTable("IT_RETURN");
            res = SapUtil.GetResult(IT_RETURN);
            
            LOGGER.info("ZBAPI_PREORD_GI   type = "+res.getT());
            LOGGER.info("ZBAPI_PREORD_GI   message = "+res.getMESSAGE());
            
            switch (res.getT()) {
                case "S":
                case "":
                    //如果过账成功则更改状态
                    ModEnquiry.ZBAPI_PREORD_STS_CHG_M(targetPrepareNo, "9");
                    break;
                default:
                    break;
            }
            
//            if (bPreparationCompleted) {
//                ModEnquiry.ZBAPI_PREORD_STS_CHG_M(targetPrepareNo, "9");
//            }
                

        } catch (JCoException e) {
            e.printStackTrace();
        }finally {
            ZBAPI_PREORD_UNLOCK(targetPrepareNo);
        }
        return res;
    }

    /*
     * 发料完成
     */
    public static Return_Result ZBAPI_PREORD_STS_CHG_M(String pPrepOrderNo, String pNewStatus) {
        Return_Result res = null;
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_PREORD_STS_CHG");
            JCoParameterList importParam = function.getImportParameterList();
            importParam.setValue("PRENO", pPrepOrderNo);
            importParam.setValue("REASON", "");
            importParam.setValue("STATUS", pNewStatus);
            importParam.setValue("FLAG", "U");
            function.execute(RfcManager.getDestination());
            res = SapUtil.GetResult(function.getExportParameterList().getStructure("RETURN"));
            LOGGER.info("ZBAPI_PREORD_STS_CHG_M----type = "+res.getT());
            LOGGER.info("ZBAPI_PREORD_STS_CHG_M----message = "+res.getMESSAGE());
//            switch (res.getT()) {
//                case "S":
//                case "":
//                    break;
//                default:
//                    break;
//            }

        } catch (JCoException e) {
            e.printStackTrace();
        } finally {
            ZBAPI_PREORD_UNLOCK(pPrepOrderNo);
        }
        return res;
    }

    
    public static List<String> getFilterLocation(String pUserType){
        List<String> list = new ArrayList<String>();
        switch (pUserType)
        {
            case "1": //Cotton 
                list.add("MFLQ");
                break;
            case "2": //Cloth
                list.add("DFLQ");
                break;
            case "3": //Trim
                list.add("FFLQ");
                break;
            default:
                break;
        }
        return list;
    }
}
